package com.slz3.business.module.goodsManagement.gooInventory.steward;

import com.slz3.business.frame.enums.AuditFlag;
import com.slz3.business.frame.enums.ValidFlag;
import com.slz3.business.frame.module.Services;
import com.slz3.business.frame.response.RR;
import com.slz3.business.module.goodsManagement.gooInventory.domain.GooInventory;
import com.slz3.business.module.goodsManagement.gooInventory.domain.GooInventoryCondition;
import org.springframework.web.reactive.function.server.ServerRequest;

import java.util.List;
import java.util.Optional;
import java.util.UUID;

public class GooInventoryResSafeValid {

    static boolean add(ServerRequest req, GooInventory gooInventory) {
        // T-O-D-O 唯一约束
        return RR.hasResErr(req);
    }

    /**
     * known: params uuids is not null
     */
    static final boolean delete(ServerRequest req, GooInventoryCondition gooInventoryCondition) {
        List<GooInventory> gooInventoryList = Services.gooInventory.findAllByUuids(gooInventoryCondition.getUuidList());
        if (gooInventoryList.stream().filter(
                item -> AuditFlag.isApproved(item.getSerAudit()) || AuditFlag.isReject(item.getSerAudit()) || ValidFlag.isDel(item.getValidFlag())
        ).count() > 0) {
            RR.resDetail(req).put(RR.SAFE_VALID, "不能删除【已审核】或【被驳回】或【被删除】状态的记录");
        } else {
            req.attributes().put(RR.RELAY_SAFE_VALID, gooInventoryList);
        }
        return RR.hasResErr(req);
    }

    static boolean modify(ServerRequest req, GooInventory gooInventory) {
        Optional<GooInventory> gooInventoryDBOpt = Services.gooInventory.single(gooInventory.getUuid());
        if (gooInventoryDBOpt.isPresent()) {
            GooInventory gooInventoryDB = gooInventoryDBOpt.get();
            if (gooInventory.getSerNum() != null
                    && !gooInventory.getSerNum().equals(gooInventoryDB.getSerNum())) {
                RR.resDetail(req).put(RR.SAFE_VALID, "Err: The number is modified.");
            } else if (!AuditFlag.isWaitSubmit(gooInventoryDB.getSerAudit())) {
                RR.resDetail(req).put(RR.SAFE_VALID, "Err: 只能修改待提交状态的记录！");
            } else {
                req.attributes().put(RR.RELAY_SAFE_VALID, gooInventoryDB);
            }
        } else {
            RR.resDetail(req).put(RR.SAFE_VALID, "cannot find entity by request uuid");
        }
        return RR.hasResErr(req);
    }

    static boolean info(ServerRequest req) {
        Optional<GooInventory> gooInventoryDB = Services.gooInventory.single(UUID.fromString(req.queryParam("uuid").get()));
        if (!gooInventoryDB.isPresent()) {
            RR.resDetail(req).put(RR.SAFE_VALID, "cannot find entity by request uuid");
        } else {
            req.attributes().put(RR.RELAY_SAFE_VALID, gooInventoryDB.get());
        }
        return RR.hasResErr(req);
    }

    /**
     * known: params uuids is not null
     */
    static final boolean auditSubmit(ServerRequest req, GooInventoryCondition gooInventoryCondition) {
        List<GooInventory> gooInventoryList = Services.gooInventory.findAllByUuids(gooInventoryCondition.getUuidList());
        if (gooInventoryList.stream().filter(item -> ValidFlag.isValid(item.getValidFlag())
                && (AuditFlag.isWaitSubmit(item.getSerAudit()))
        ).count() > 0) {
            req.attributes().put(RR.RELAY_SAFE_VALID, gooInventoryList);
        } else {
            RR.resDetail(req).put(RR.SAFE_VALID, "不能提交【被删除】状态或不是【待提交】状态的记录");
        }
        return RR.hasResErr(req);
    }

    /**
     * known: params uuids is not null
     */
    static final boolean audit(ServerRequest req, GooInventoryCondition gooInventoryCondition) {
        List<GooInventory> gooInventoryList = Services.gooInventory.findAllByUuids(gooInventoryCondition.getUuidList());
        if (gooInventoryList.stream().filter(
                item -> !AuditFlag.isWaitAudit(item.getSerAudit()) || ValidFlag.isDel(item.getValidFlag())
        ).count() > 0) {
            RR.resDetail(req).put(RR.SAFE_VALID, "不能审核【被删除】状态或不是【待审核】状态的记录");
        } else {
            req.attributes().put(RR.RELAY_SAFE_VALID, gooInventoryList);
        }
        return RR.hasResErr(req);
    }

    /**
     * known: params uuids is not null
     */
    static final boolean auditReject(ServerRequest req, GooInventoryCondition gooInventoryCondition) {
        List<GooInventory> gooInventoryList = Services.gooInventory.findAllByUuids(gooInventoryCondition.getUuidList());
        if (gooInventoryList.stream().anyMatch(item -> (ValidFlag.isDel(item.getValidFlag())
                || AuditFlag.isWaitSubmit(item.getSerAudit())
                || AuditFlag.isApproved(item.getSerAudit())
                || AuditFlag.isReject(item.getSerAudit())
        ))) {
            RR.resDetail(req).put(RR.SAFE_VALID, "不能驳回【被删除】【待提交】【已审核】【被驳回】状态的记录");
        } else {
            req.attributes().put(RR.RELAY_SAFE_VALID, gooInventoryList);
        }
        return RR.hasResErr(req);
    }

    static final boolean usingEnable(ServerRequest req, GooInventoryCondition gooInventoryCondition) {
        List<GooInventory> gooInventoryList = Services.gooInventory.findAllByUuids(gooInventoryCondition.getUuidList());
        if (gooInventoryList.stream().filter(
                item -> !AuditFlag.isWaitAudit(item.getSerAudit()) || ValidFlag.isDel(item.getValidFlag())
        ).count() > 0) {
            RR.resDetail(req).put(RR.SAFE_VALID, "不能启用【被删除】状态");
        } else {
            req.attributes().put(RR.RELAY_SAFE_VALID, gooInventoryList);
        }
        return RR.hasResErr(req);
    }

    static final boolean usingDisable(ServerRequest req, GooInventoryCondition gooInventoryCondition) {
        List<GooInventory> gooInventoryList = Services.gooInventory.findAllByUuids(gooInventoryCondition.getUuidList());
        if (gooInventoryList.stream().filter(
                item -> !AuditFlag.isWaitAudit(item.getSerAudit()) || ValidFlag.isDel(item.getValidFlag())
        ).count() > 0) {
            RR.resDetail(req).put(RR.SAFE_VALID, "不能禁用【被删除】状态");
        } else {
            req.attributes().put(RR.RELAY_SAFE_VALID, gooInventoryList);
        }
        return RR.hasResErr(req);
    }

    static final boolean sync(ServerRequest req, GooInventoryCondition gooInventoryCondition) {
//        List<GooInventory> gooInventoryList = Services.gooInventory.findAllByUuids(gooInventoryCondition.getUuidList());
//        if (gooInventoryList.stream().filter(
//                item -> !AuditFlag.isWaitAudit(item.getSerAudit()) || ValidFlag.isDel(item.getValidFlag())
//        ).count() > 0) {
//            RR.resDetail(req).put(RR.SAFE_VALID, "不能禁用【被删除】状态");
//        } else {
//            req.attributes().put(RR.RELAY_SAFE_VALID, gooInventoryList);
//        }
        return RR.hasResErr(req);
    }
}
